<template>
  <div>
    <Row class="search-info-style">
      <Col span="6">
        <span>姓&ensp;&ensp;&ensp;名&ensp;：</span>
        <Input v-model="params.name" placeholder="姓名" style="width:70%" />
      </Col>
      <Col span="6">
        <span>证件号码：</span>
        <Input v-model="params.card_no" placeholder="证件号码" style="width:70%" />
      </Col>
      <Col style="float:right" pull="1">
        <Button class="new-btn-sty" @click="findtime()"><Icon type="ios-search" /> 搜索</Button>
        <Button class="new-btn-sty" type="info" @click="add()"><Icon type="md-add" />添加</Button>
      </Col>
    </Row>
<!--    <Button type="info" size="small" @click="add()" style="margin-top: 5px">添加</Button>-->
    <Table border :columns="columns1" :data="data1"></Table>
    <Row v-if="loadData == true">
      <Col class="demo-spin-col" span="8" offset="8">
        <Spin fix>
          <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
          <div>数据正在加载</div>
        </Spin>
      </Col>
    </Row>
    <Page :total="totalnum" :current="currentPageNo" :page-size="currentPageSize" @on-change="matterPage" class="page_style" show-sizer @on-page-size-change="SXsize"/>
    <Modal title="人员信息" v-model="personInfoShow" @on-ok="createPersonInfo" @on-cancel="personInfoShow=false" width="700">
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">姓名:</Col>
        <Col span="10" offset="1">
          <Input v-model="paramData.name" class="div-search" placeholder="请输入姓名"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">联系方式:</Col>
        <Col span="10" offset="1">
          <Input v-model="paramData.mobile" class="div-search" placeholder="请输入联系方式"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">证件号码:</Col>
        <Col span="10" offset="1">
          <Input v-model="paramData.card_no" class="div-search" placeholder="请输入证件号码"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">出发地省:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.originate_province" placeholder="请选择出发地省" filterable @on-change="chaneProv(1)">
            <Option v-for="obj in inoutCity.provinceList" :value="obj.name" :key="obj.code">{{obj.name}}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">出发地市:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.originate_city" placeholder="请选择出发地城市" filterable>
            <Option v-for="obj in inoutCity.city1" :value="obj.name" :key="obj.code">{{obj.name}}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="">
        <Col span="7" style="text-align: right" class="">出发地具体地址:</Col>
        <Col span="10" offset="1">
          <Input type="textarea" v-model="paramData.originate_address" class="div-search" placeholder="请输入出发地具体地址"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">返乡人数:</Col>
        <Col span="10" offset="1">
          <Input type="number" v-model="paramData.user_num" class="div-search" placeholder="请输入返乡人数"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">返乡时间:</Col>
        <Col span="10" offset="1">
          <DatePicker type="date" placeholder="选择日期" style="width: 100%" v-model="paramData.arrive_time"></DatePicker>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">交通方式:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.arrive_type" placeholder="请选择交通方式">
            <Option value="1">火车</Option>
            <Option value="2">轮船</Option>
            <Option value="3">飞机</Option>
            <Option value="4">大巴</Option>
            <Option value="5">自驾</Option>
          </Select>
        </Col>
      </Row>
      <Row class="">
        <Col span="7" style="text-align: right" class="">交通方式备注:</Col>
        <Col span="10" offset="1">
          <Input type="textarea" v-model="paramData.arrive_type_remark" class="div-search" placeholder="请输入交通方式备注"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">接触史:</Col>
        <Col span="10" offset="1">
          <Input v-model="paramData.contact_history" class="div-search" placeholder="请输入接触史"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">发热史:</Col>
        <Col span="10" offset="1">
          <Input v-model="paramData.fever_history" class="div-search" placeholder="请输入发热史"></Input>
        </Col>
      </Row>

      <Row class="item1">
        <Col span="7" style="text-align: right" class="">住所区/县:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.area" placeholder="区/县" filterable @on-change="perChange(1, false)">
            <Option v-for="obj in addressInfo.list1" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">住所街道:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.street" placeholder="街道" filterable @on-change="perChange(2, false)">
            <Option v-for="obj in addressInfo.list2" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">住所社区:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.community" placeholder="社区" filterable>
            <Option v-for="obj in addressInfo.list3" :value="obj.title" :key="obj.id">{{obj.title}}</Option>
          </Select>
        </Col>
      </Row>

      <Row class="">
        <Col span="7" style="text-align: right" class="">返沈详细住址:</Col>
        <Col span="10" offset="1">
          <Input type="textarea" v-model="paramData.for_address" class="div-search" placeholder="XX区XX街道XX社区XX单元"></Input>
        </Col>
      </Row>
      <Row class="item1">
        <Col span="7" style="text-align: right" class="">来沈原因:</Col>
        <Col span="10" offset="1">
          <Select v-model="paramData.arrive_reason" multiple placeholder="请选择来沈原因" @on-change="arriveReasonChage()">
            <Option value="出差">出差</Option>
            <Option value="就医">就医</Option>
            <Option value="就学">就学</Option>
            <Option value="就业">就业</Option>
            <Option value="旅游">旅游</Option>
            <Option value="探亲">探亲</Option>
            <Option value="访友">访友</Option>
            <Option value="返乡">返乡</Option>
            <Option value="信访">信访</Option>
            <Option value="其他">其他</Option>
          </Select>
        </Col>
      </Row>
      <Row class="" v-if="arrive_reason_remark_show">
        <Col span="7" style="text-align: right" class="">来沈原因备注:</Col>
        <Col span="10" offset="1">
          <Input type="textarea" v-model="paramData.arrive_reason_remark" class="div-search" placeholder="请输入来沈原因备注"></Input>
        </Col>
      </Row>
    </Modal>
  </div>
</template>
<script>
import { date2str, grid_downlist } from '@/api/eventlist'
import { getCityList } from '@/api/common'
import { findList, saveAntiRuralInfo, getAntiRuralInfoByID } from '@/api/yi-qing-fang-kong'
export default {
  data () {
    return {
      arrive_reason_remark_show: false,
      company_type: '1',
      addressInfo: { list1: [], list2: [], list3: [] },
      totalnum: 0,
      currentPageNo: 1,
      currentPageSize: 10,
      loadData: false,
      personInfoShow: false,
      inoutCity: { provinceList: [], city1: [], city2: [] },
      params: {},
      paramData: {},
      data1: [],
      columns1: [
        { title: '序号', type: 'index', width: 60, align: 'center', fixed: 'left' },
        { title: '姓名', key: 'name', width: 100, align: 'center', fixed: 'left' },
        { title: '证件号码', key: 'card_no', width: 150, align: 'center' },
        { title: '联系方式', key: 'mobile', width: 100, align: 'center' },
        { title: '出发地省', key: 'originate_province', width: 100, align: 'center' },
        { title: '出发地市', key: 'originate_city', width: 100, align: 'center' },
        { title: '出发地详细地址', key: 'originate_address', width: 100, align: 'center' },
        { title: '返乡人数', key: 'user_num', width: 100, align: 'center' },
        { title: '返乡时间', key: 'arrive_time', width: 150, align: 'center' },
        // { title: '交通方式', key: 'arrive_type', width: 100, align: 'center' },
        // { title: '交通方式详情', key: 'arrive_type_remark', width: 100, align: 'center' },
        // { title: '接触式', key: 'contact_history', width: 100, align: 'center' },
        // { title: '发热史', key: 'fever_history', width: 100, align: 'center' },
        // { title: '探望人地址', key: 'for_address', width: 100, align: 'center' },
        {
          title: '编辑',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          minWidth: 150,
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'primary', size: 'small' },
                style: { marginRight: '5px', backgroundColor: '#598ED1', borderColor: '#598ED1' },
                on: {
                  click: () => {
                    this.editNotice(params.row)
                  }
                }
              }, '编辑')
            )
            return h('div', temp)
          }
        }
      ]
    }
  },
  methods: {
    init () {
      this.params.pageNo = this.currentPageNo
      this.params.pageSize = this.currentPageSize
      this.data1 = []
      this.loadData = true
      findList(this.params).then(res => {
        if (res.data.errcode === 0) {
          this.data1 = res.data.data.results
          this.loadData = false
        } else {
          this.loadData = false
          this.$Notice.error({ title: '获取列表失败', desc: res.errmsg })
        }
      })
    },
    add () {
      this.personInfoShow = true
    },
    createPersonInfo () {
      this.paramData.arrive_time = date2str(this.paramData.arrive_time)
      this.paramData.arrive_reason = this.paramData.arrive_reason.join(',')
      saveAntiRuralInfo(this.paramData).then(res => {
        if (res.data.errcode === 0) {
          this.$Notice.success({
            title: '成功'
            // desc: '送检箱子创建成功' + res.data.data.success_num + '个样瓶'
          })
          this.init()
        } else {
          this.$Notice.error({ title: '失败', desc: '' })
          this.init()
        }
      })
    },
    editNotice (item) {
      getAntiRuralInfoByID(item).then(res => {
        if (res.data.errcode === 0) {
          this.personInfoShow = true
          this.paramData = res.data.data
          this.getCity(null, 'provinceList', true)
          this.getDownAddress(0, 1, true)
          if (this.paramData.arrive_reason) {
            let arrive_reasons = this.paramData.arrive_reason.split(',')
            this.paramData.arrive_reason = arrive_reasons
            this.arriveReasonChage()
          } else {
            this.paramData.arrive_reason = []
          }
        } else {
          this.$Notice.error({ title: '获取人员信息失败', desc: '' })
          this.init()
        }
      })
    },
    arriveReasonChage () {
      let arrive_reason_remark_show = false
      this.paramData.arrive_reason.forEach((item, index) => {
        if (item === '其他') {
          arrive_reason_remark_show = true
        }
      })
      console.log('arrive_reason_remark_show', arrive_reason_remark_show)
      this.arrive_reason_remark_show = arrive_reason_remark_show
    },
    perChange (index, flag) {
      if (index === 3 || (this.company_type === '2' && index === 2)) {
        return
      }
      let temparea = ''
      if (index === 1) {
        temparea = this.paramData['area']
      }
      if (index === 2) {
        temparea = this.paramData['street']
      }
      if (!temparea) {
        return
      }
      let templist = this.addressInfo['list' + index]
      let tempdata = {}
      for (let i = 0; i < templist.length; i++) {
        if (temparea === templist[i].title) {
          tempdata = templist[i]
          break
        }
      }
      this.getDownAddress(tempdata.id, index + 1, flag)
    },
    getDownAddress (pid, index, flag) {
      let param = {
        pid: pid,
        pageNo: 1,
        pageSize: 200
      }
      grid_downlist(param).then(res => {
        if (res.data.data) {
          console.log(res.data.data.results, '000000')
          this.addressInfo['list' + index] = res.data.data.results
          if (flag) {
            this.perChange(index, flag)
          }
        } else {
          this.$Notice.error({ title: '查看失败', desc: '查询地址信息失败' })
        }
      })
    },
    chaneProv (index) {
      let temparea = this.paramData['originate_province']
      console.log(temparea)
      if (!temparea) {
        return
      }
      let templist = this.inoutCity.provinceList
      let tempdata = {}
      for (let i = 0; i < templist.length; i++) {
        if (temparea === templist[i].name) {
          tempdata = templist[i]
          break
        }
      }
      console.log(tempdata.code)
      this.getCity(tempdata.code, 'city' + index, false)
    },
    getCity (code, listname, flag) {
      getCityList({ code: code }).then(res => {
        if (res.data.errcode === 0) {
          this.inoutCity[listname] = res.data.data
          if (flag) {
            if (this.paramData.originate_city) {
              this.chaneProv(1)
            }
            if (this.paramData.originate_city) {
              this.chaneProv(2)
            }
          }
        } else {
          this.$Notice.error({ title: '查询失败' })
        }
      })
    },
    matterPage (pageNo) {
      this.currentPageNo = pageNo
      this.init()
    },
    findtime () {
      this.currentPageNo = 1
      this.currentPageSize = 10
      this.init()
    },
    SXsize (pageSize) {
      this.currentPageSize = pageSize
      this.init()
    }
  },
  mounted () {
    this.init()
    this.getCity(null, 'provinceList', false)
    this.getDownAddress(0, 1, false)
  }
}
</script>
<style>
  .page_style {
    margin-top: 10px;
    text-align: right;
  }
  .item1{
    height: 42px;
    line-height: 40px;
  }
  .new-btn-sty {
    margin-right: 5px;
    color: #fff;
    background-color: #235CA7;
    border-color: #235CA7;
    font-weight: 500;
  }
  .search-info-style {
    margin: 10px;
  }
</style>
